/**
 * Created by WLG on 2018/11/9.
 */
//let _ = require('lodash');
let BaseSocketHandler = require("./baseSocketHandler");
let serverConfig = require('../config/serverConfig');
let SystemConst = require("../config/systemConst");
let redisDB = require('../lib/redisManage');
let QueryData = require('../model/sysQueryData');

let AccountMemService = require('../services/accountMemService');
let OnlineService = require('../services/onlineService');
let ThirdApiService = require('../services/thirdApiService');
/**
 * 系统处理
 */
class SystemSocketHandler extends BaseSocketHandler {
    static attach(io, socket){
        //登录
        socket.on(SystemConst.EventName.SystemSocket.ClientLogin,async (data)=>{
            let username = data.data.username;
            let terminalType = data.data.terminalType;
            if(!terminalType){
                terminalType = 'other';
            }
            let socketID = socket.id;
            let am = await OnlineService.memLogin(io, username, terminalType, socketID);
            let qd = new QueryData();
            qd.data = am;
            qd.smid = data.smid;
            await socket.emit(SystemConst.EventName.SystemSocket.ServerLogined,qd);
            commonUtil.consoleTestInfo("用户["+am.name+"]登录");
        });
        socket.on(SystemConst.EventName.SystemSocket.ClientLoginByImToken,async (data)=>{

            let imToken = data.data.imToken;
            let imInitMode = await ThirdApiService.getImInitModeCache(imToken);
            if(imInitMode){
                let username = imInitMode.memInfo.username;
                let terminalType = data.data.terminalType;
                if(!terminalType){
                    terminalType = 'other';
                }
                let socketID = socket.id;
                let am = await OnlineService.memLogin(io, username, terminalType, socketID);
                let qd = new QueryData();
                qd.data = imInitMode;
                qd.smid = data.smid;
                await socket.emit(SystemConst.EventName.SystemSocket.ServerLoginedByImToken,qd);
                commonUtil.consoleTestInfo("用户["+am.name+"]登录 By ImToken");
            }
        });



        socket.on('disconnect',async (reason) => {
            await OnlineService.removeSocketInfo(socket.id);
            //commonUtil.consoleRed('disconnect:',socket.id);
        });
    };
}
module["exports"] = SystemSocketHandler;